#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2018/9/19 18:05
@desc
"""
# 导入Thrift模块
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

# 导入自己生成的hbase python模块
from hbasepython.hbase import THBaseService
from hbasepython.hbase.ttypes import *

# 创建 Socket连接，到CSparkMaster:9090
transport = TSocket.TSocket('CSparkMaster', 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = THBaseService.Client(protocol)

# 打开传输端口
transport.open()

# put 操作 插入数据
# 创建表t1，并增加3个列族 create 't1', {NAME => 'f1', VERSIONS => 1}, {NAME => 'f2', VERSIONS => 1}, {NAME => 'f3', VERSIONS => 1}
table = b'default:t1'
row = b'row1'
v1 = TColumnValue(b'f1', b'id', b'101')
v2 = TColumnValue(b'f1', b'name', b'tomas')
v3 = TColumnValue(b'f1', b'age', b'12')
vals = [v1, v2, v3]
# 指定行和列
tput = TPut(row, vals)
# 调用put执行插入
client.put(table, tput)
print("okkkkkk")

# 查询
col_id = TColumn(b'f1', b'id')
col_name = TColumn(b'f1', b'name')
col_age = TColumn(b'f1', b'age')

cols = [col_id, col_name, col_age]
tget = TGet(row, cols)
r = client.get(table, tget)

rs = r.columnValues

print(rs)

for ele in rs:
    print("The Value is:" + bytes.decode((ele.value)))

# 删除
deleteCols = [col_id, col_age]
tdelete = TDelete(row, deleteCols)
res = client.deleteSingle(table, tdelete)
print(res)
transport.close()
